//
//  ZJAddBankVC.m
//  这家测试项目
//
//  Created by IOS_DEVELOPER on 2017/7/10.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJAddBankVC.h"
#import "ZJPickerView.h"

@interface ZJAddBankVC ()<ZJPickerViewDelegate,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *chiKa;
@property (weak, nonatomic) IBOutlet UILabel *bankName;
@property (weak, nonatomic) IBOutlet UILabel *kaHao;
@property (weak, nonatomic) IBOutlet UITextField *chiKaNameTX;
@property (weak, nonatomic) IBOutlet UIButton *changeBankBtn;
@property (weak, nonatomic) IBOutlet UITextField *cardNameTX;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UIButton *doBtn;
@property (weak, nonatomic) IBOutlet UIView *bgView;
@property (weak, nonatomic) IBOutlet UILabel *idoLebel;
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;

@end

@implementation ZJAddBankVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.cardNameTX.delegate = self;
    self.changeBankBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [self.leftBtn setImage:[UIImage imageWithContentsOfFile:[Utils imgPath:@"tqbm_icon_xy@3x.png"]] forState:0];
    [self.doBtn setTitleColor:[Utils colorWithHexString:allConstHexColor] forState:0];
    [self.sureBtn setBackgroundColor:[Utils colorWithHexString:allConstHexColor]];
    if ([self.type isEqualToString:@"支付宝"]) {
        self.chiKa.text = @"姓名";
        self.kaHao.text = @"账号";
        self.changeBankBtn.hidden = YES;
        self.bankName.hidden = YES;
         self.name.text = self.model.real_name;
        
        self.kaHao.sd_layout.leftSpaceToView(self.bgView, 12).topSpaceToView(self.chiKa, 32).widthIs(68).heightIs(21);
        self.cardNameTX.sd_layout.leftSpaceToView(self.kaHao, 8).topSpaceToView(self.name, 24).rightSpaceToView(self.bgView, 8).heightIs(30);
        self.cardNameTX.placeholder = @"请输入账号";
        self.bgView.sd_layout.heightIs(103);
        [self.bankName removeFromSuperview];
        [self.changeBankBtn removeFromSuperview];
    }
    if ([self.type isEqualToString:@"银行卡"]) {
        self.name.text = self.model.real_name;
    }
    if ([self.type isEqualToString:@"删除"]) {
        
        NSString *name = [self.model.real_name substringFromIndex:(self.model.real_name.length - 1)];
        [self.changeBankBtn setTitle:[NSString stringWithFormat:@"*%@",name] forState:0];
        [self.changeBankBtn setTitleColor:[UIColor blackColor] forState:0];
        self.changeBankBtn.userInteractionEnabled = NO;
        self.bankName.text = @"姓名";
        
        self.kaHao.text = @"手机号";
        self.cardNameTX.userInteractionEnabled = NO;
        self.doBtn.hidden = YES;
        [self.sureBtn setTitle:@"删除" forState:0];
        self.title = @"账户详情";

        self.idoLebel.hidden = YES;
        self.leftBtn.hidden = YES;
        NSString *leftPhone = [self.model.mobile substringToIndex:3];
        NSString *rightPhone = [self.model.mobile substringFromIndex:(self.model.mobile.length - 4)];
        self.cardNameTX.text = [NSString stringWithFormat:@"%@****%@",leftPhone,rightPhone];
        
        self.chiKa.hidden = YES;
        UIImageView * imgV = [[UIImageView alloc]initWithFrame:CGRectMake(12, 13, 30, 30)];
        
        if ([self.model.bank isEqualToString:@"支付宝"]) {
            imgV.image = [UIImage imageNamed:@"yhgl_icon_zfb"];
        }
        if ([self.model.bank isEqualToString:@"中国银行"]) {
            imgV.image = [UIImage imageNamed:@"yhgl_icon_zgyh"];
        }
        if ([self.model.bank isEqualToString:@"中国农业银行"]) {
            imgV.image = [UIImage imageNamed:@"yhgl_icon_nyyh"];
        }
        if ([self.model.bank isEqualToString:@"中国工商银行"]) {
            imgV.image = [UIImage imageNamed:@"wdzh_icon_yh"];
        }
        if ([self.model.bank isEqualToString:@"中国建设银行"]) {
            imgV.image = [UIImage imageNamed:@"yhgl_icon_jsyh"];
        }
        
        [self.bgView addSubview:imgV];
        
        if ([self.model.bank isEqualToString:@"支付宝"]) {
            self.name.text = [NSString stringWithFormat:@"%@(%@)",self.model.bank,self.model.account];
        }else{
        NSString *num = [self.model.account substringFromIndex:(self.model.account.length - 4)];
        self.name.text = [NSString stringWithFormat:@"%@(尾号%@)",self.model.bank,num];
        }
    }
    

}

- (IBAction)changeBankBtn:(UIButton *)sender {
    
    ZJPickerView * vc = [[ZJPickerView alloc]init];
    vc.delegate = self;
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    vc.definesPresentationContext = YES;
    
    [self presentViewController:vc animated:YES completion:nil];
}

-(void)clickBank:(NSString *)str{
    [self.changeBankBtn setTitle:str forState:0];
}

- (IBAction)sureBtn:(UIButton *)sender {
    
    if ([self.type isEqualToString:@"支付宝"]) {
        
        self.changeBankBtn.titleLabel.text = @"支付宝";
        
        if ([NSString isNullOrEmpty:self.name.text]) {
            [HUD showMsg:@"请输入姓名"]; return;
        }
        if ([NSString isNullOrEmpty:self.cardNameTX.text]) {
            [HUD showMsg:@"请输入账号"]; return;
        }
        if (![Utils checkContent:self.cardNameTX.text regex:REGEX_EMAIL] && ![Utils checkContent:self.cardNameTX.text regex:REGEX_PHONE_DEFAULT]) {
            [HUD showMsg:@"请输入正确的账号"]; return;
        }
        
    }else if ([self.type isEqualToString:@"银行卡"]){
        
        if ([NSString isNullOrEmpty:self.name.text]) {
            [HUD showMsg:@"请输入姓名"]; return;
        }
        if ([NSString isNullOrEmpty:self.cardNameTX.text]) {
            [HUD showMsg:@"请输入卡号"]; return;
        }
        if (![Utils checkContent:self.cardNameTX.text regex:REGEX_BANK_CARD]) {
            [HUD showMsg:@"请输入正确的卡号"]; return;
        }
        if ([self.sureBtn.titleLabel.text isEqualToString:@"请选择银行"]) {
            [HUD showMsg:@"请输入银行名称"]; return;
        }
        
    }
    
    [HUD showLoading:@"提交中..."];
    
    if ([self.type isEqualToString:@"支付宝"] || [self.type isEqualToString:@"银行卡"]){

        NSDictionary *parameter = [LRNetwork requestParameter:@{@"bank":self.changeBankBtn.titleLabel.text?:@"支付宝",
                                                            @"account":self.cardNameTX.text}];
    NSString *url = [LRNetwork requestURL:@"cgi/bank_card_add.php"];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        
        if (self.changeBankBtn.titleLabel.text.length > 1) {
            self.model.bank = self.changeBankBtn.titleLabel.text;
        }else{
            self.model.bank = @"支付宝";
        }
        self.model.account = self.cardNameTX.text;
        SYN_USER(self.model);
        [HUD showSucceedMsg:@"绑定成功!"];
        [self.navigationController popViewControllerAnimated:YES];
        
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD showErrorMsg:errorMessage];
    } withFailureBlock:^{
        
    }];
        
    }else if ([self.type isEqualToString:@"删除"]){
    
        NSDictionary *parameter = [LRNetwork requestParameter:@{}];
        NSString *url = [LRNetwork requestURL:@"cgi/bank_card_del.php"];
        
        [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
            
            self.model.bank = @"";
            self.model.account = @"";
            SYN_USER(self.model);
            [HUD showSucceedMsg:@"删除成功!"];
            [self.navigationController popViewControllerAnimated:YES];
            
        } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
            [HUD showErrorMsg:errorMessage];
        } withFailureBlock:^{
            
        }];
    
    }
}

- (IBAction)xieyiBtn:(UIButton *)sender {
    
    ZJActivityVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJActivityVC"];
    vc.urlStr = agreement_zhejia100;
    [self.navigationController pushViewController:vc animated:YES];
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.cardNameTX && ![self.type isEqualToString:@"支付宝"]) {
        if (self.cardNameTX.text.length > 19) {
            return NO;
        }
    }
    return YES;
}
@end
